package com.zygh.policeasset.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.mapper.AuditFlowMapper;
import com.zygh.policeasset.mapper.ContractMapper;
import com.zygh.policeasset.mapper.ContractPayMapper;
import com.zygh.policeasset.mapper.ContractPayMethodMapper;
import com.zygh.policeasset.mapper.UploadFileMapper;
import com.zygh.policeasset.pojo.Contract;
import com.zygh.policeasset.pojo.ContractPay;
import com.zygh.policeasset.pojo.ContractPayMethod;
import com.zygh.policeasset.pojo.Project;
import com.zygh.policeasset.pojo.ProjectNameType;
import com.zygh.policeasset.pojo.Section;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.UploadFile;
import com.zygh.policeasset.utils.AuditUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/ContractService.class */
public class ContractService {
    @Autowired
    ContractMapper contractMapper;
    @Autowired
    ContractPayMapper contractPayMapper;
    @Autowired
    UploadFileMapper uploadFileMapper;
    @Autowired
    ContractPayMethodMapper contractPayMethodMapper;
    @Autowired
    AuditFlowMapper auditFlowMapper;
    @Autowired
    AuditFlowService auditFlowService;

    public PageInfo findByProject(String no, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.contractMapper.findByProject(no));
    }

    public List<Contract> findByProjectNoPage(String no) {
        return this.contractMapper.findByProject(no);
    }

    public List<ProjectNameType> selectBytype() {
        return this.contractMapper.selectBytype();
    }

    public List<ProjectNameType> findSectionByType(String projectNo) {
        return this.contractMapper.findSectionByType(projectNo);
    }

    public PageInfo findAll(String name, Integer status, Integer companyId,
                            Integer pageNum, Integer pageSize, String time1,
                            String time2, Integer category, Integer userId) {
        PageHelper.startPage(pageNum.intValue(), pageSize.intValue());
        List<Contract> list = this.contractMapper.findAll(name, status, companyId,
                time1, time2, category, userId);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setContractPayMethodList(this.contractPayMethodMapper.findByProjectAndSection(list.get(i).getProjectNo(), Integer.valueOf(list.get(i).getId())));
                if(!StringUtils.isEmpty(list.get(i).getMoney())){
                    BigDecimal bd = new BigDecimal(list.get(i).getMoney());
                    bd = bd.setScale(2, RoundingMode.HALF_UP);
                    list.get(i).setMoney(bd.toString());
                }else {
                    list.get(i).setMoney("0");
                }
            }
        }
        return new PageInfo<>(list);
    }

    public Contract findById(Integer id) {
        Contract contract = this.contractMapper.findById(id);
        contract.setContractPayMethodList(this.contractPayMethodMapper.findByProjectAndSection(contract.getProjectNo(), Integer.valueOf(contract.getId())));
        return contract;
    }

    public Integer findCount() {
        return this.contractMapper.findCount();
    }

    public PageInfo findcontractpay(String name, Integer status, Integer companyId, Integer pageNum, Integer pageSize, String time1, String time2, Integer category) {
        PageHelper.startPage(pageNum.intValue(), pageSize.intValue());
        List<Contract> list = this.contractMapper.findAll(name, status, companyId, time1, time2, category, null);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setContractPayList(this.contractPayMapper.findByProjectAndSection(
                    list.get(i).getProjectNo(), Integer.valueOf(list.get(i).getId())));
        }
        return new PageInfo<>(list);
    }

    public PageInfo findByStatus(int status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.contractMapper.findByStatus(status));
    }

    public List<Project> getProject() {
        return this.contractMapper.getProject();
    }

    public List<Section> getAdd(String projectNo) {
        return this.contractMapper.getAdd(projectNo);
    }

    public Integer add(Contract contract) {
        String now = DateUtil.now();
        String flowNo = AuditUtil.getFlowNo(now);
        contract.setFlowNo(flowNo);
        this.auditFlowService.add(contract.getUserId(), flowNo, now,
                TableType.CONTRACT.getName(), TableType.CONTRACT.getType(),
                contract.getProjectNo(), contract.getSectionId());
        try {
            this.contractMapper.add(contract);
            List<UploadFile> uploadFileList = contract.getUploadFileList();
            for (int i = 0; i < uploadFileList.size(); i++) {
                uploadFileList.get(i).setProjectId(contract.getId());
                uploadFileList.get(i).setProjectName("contract");
                uploadFileList.get(i).setCreateDate(DateUtil.now());
                this.uploadFileMapper.add(uploadFileList.get(i));
            }
            List<ContractPayMethod> contractPayList = contract.getContractPayMethodList();
            for (int i2 = 0; i2 < contractPayList.size(); i2++) {
                contractPayList.get(i2).setContractPayNum(Integer.valueOf(i2 + 1));
                contractPayList.get(i2).setContractPayId(contract.getProjectNo());
                contractPayList.get(i2).setContractPaySection(Integer.valueOf(contract.getId()));
                this.contractPayMethodMapper.add(contractPayList.get(i2));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(contract.getId());
    }

    public Integer update(Contract contract) {
        int res = 0;
        try {
            res = this.contractMapper.update(contract).intValue();
            List<ContractPayMethod> contractPayList = contract.getContractPayMethodList();
            for (int i = 0; i < contractPayList.size(); i++) {
                if (contractPayList.get(i).getId() != 0) {
                    this.contractPayMethodMapper.update(contractPayList.get(i));
                } else {
                    contractPayList.get(i).setContractPayId(contract.getProjectNo());
                    contractPayList.get(i).setContractPaySection(Integer.valueOf(contract.getId()));
                    this.contractPayMethodMapper.add(contractPayList.get(i));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

    public Integer updatePay(Contract contract) {
        int res = 0;
        try {
            res = this.contractMapper.update(contract).intValue();
            List<ContractPay> contractPayList = contract.getContractPayList();
            for (int i = 0; i < contractPayList.size(); i++) {
                if (contractPayList.get(i).getId() != 0) {
                    this.contractPayMapper.update(contractPayList.get(i));
                } else {
                    contractPayList.get(i).setContractPayId(contract.getProjectNo());
                    contractPayList.get(i).setContractPaySection(Integer.valueOf(contract.getId()));
                    this.contractPayMapper.add(contractPayList.get(i));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

    public Integer remove(int id) {
        int res = 0;
        try {
            res = this.contractMapper.remove(id).intValue();
            this.uploadFileMapper.delete(id, "tender");
            this.contractPayMethodMapper.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.valueOf(res);
    }

    public Integer updateStatus(int id, int status) {
        return this.contractMapper.updateStatus(id, status);
    }

    public List<Contract> toExcel(Integer id, String name, String companyId, String time1, String time2) {
        return this.contractMapper.toExcel(id, name, companyId, time1, time2);
    }

    public boolean checkTime(String confirmTime) {
        boolean flag = false;
        if (DateUtil.between(new DateTime(), DateUtil.parse(confirmTime), DateUnit.DAY) > 30) {
            flag = true;
        }
        return flag;
    }
}